﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace LabLibary.ExtractData
{
    public static class MissionU120
    {
        public static List<ObjResults> ExtactData(string fileName)
        {
            try
            {
                List<ObjResults> _lstResults = new List<ObjResults>();
                ObjResults _objResults = new ObjResults();

                DateTime _Ngay = DateTime.Now;
                string _May = "12";
                string _SID = string.Empty;
                string _LabMaChineId = string.Empty;
                string _Value = string.Empty;

                string result = string.Empty;
                try
                {
                    using (StreamReader sr = new StreamReader(fileName))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {

                            if (line.Contains("Date"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "Date:");
                                if (_tmpSplit.Length > 1)
                                {
                                    string[] _date = Regex.Split(_tmpSplit[1], "-");

                                    _Ngay = new DateTime(int.Parse(_date[2]), int.Parse(_date[1]), int.Parse(_date[0]), 0, 0, 0);
                                }
                            }


                            else if (line.Contains("ID"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "ID:");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "ID: " + _tmpSplit[1] + "\n";

                                    _SID = _tmpSplit[1];
                                }
                            }

                            else if (line.Contains("Date"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "Date:");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "Date: " + _tmpSplit[1] + "\n";
                                }
                            }

                            else if (line.Contains("LEU"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "LEU");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "LEU: " + _tmpSplit[1].Trim() + "\n";

                                    string _value = _tmpSplit[1].Trim();

                                    //_lstResults.Add(new ObjResults(_SID, "LEU", _tmpSplit[1].Trim()));
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "LEU", _value, DateTime.Now));
                                }
                            }

                            else if (line.Contains("NIT"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "NIT");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "NIT: " + _tmpSplit[1].Trim() + "\n";

                                    string _value = _tmpSplit[1].Trim();

                                    //_lstResults.Add(new ObjResults(_SID, "NIT", _tmpSplit[1].Trim()));
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "NIT", _value, DateTime.Now));
                                }
                            }

                            else if (line.Contains("URO"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "URO");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "URO: " + _tmpSplit[1].Trim() + "\n";

                                    string _value = _tmpSplit[1].Trim();
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "URO", _value, DateTime.Now));
                                }
                            }

                            else if (line.Contains("PRO"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "PRO");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "PRO: " + _tmpSplit[1].Trim() + "\n";

                                    string _value = _tmpSplit[1].Trim();
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "PRO", _value, DateTime.Now));
                                }
                            }

                            else if (line.Contains("pH"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "pH");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "pH: " + _tmpSplit[1].Trim() + "\n";

                                    //_lstResults.Add(new ObjResults(_SID, "pH", _tmpSplit[1].Trim()));

                                    string _value = _tmpSplit[1].Trim();
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "pH", _value, DateTime.Now));
                                }
                            }

                            else if (line.Contains("BLO"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "BLO");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "BLO: " + _tmpSplit[1].Trim() + "\n";

                                    //_lstResults.Add(new ObjResults(_SID, "BLO", _tmpSplit[1].Trim()));

                                    string _value = _tmpSplit[1].Trim();
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "BLO", _value, DateTime.Now));
                                }
                            }

                            else if (line.Contains("SG"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "SG");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "SG: " + _tmpSplit[1].Trim() + "\n";

                                    //_lstResults.Add(new ObjResults(_SID, "SG", _tmpSplit[1].Trim()));

                                    string _value = _tmpSplit[1].Trim();
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "SG", _value, DateTime.Now));
                                }
                            }

                            else if (line.Contains("KET"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "KET");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "KET: " + _tmpSplit[1].Trim() + "\n";

                                    //_lstResults.Add(new ObjResults(_SID, "KET", _tmpSplit[1].Trim()));

                                    string _value = _tmpSplit[1].Trim();
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "KET", _value, DateTime.Now));

                                }
                            }

                            else if (line.Contains("BIL"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "BIL");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "BIL: " + _tmpSplit[1].Trim() + "\n";

                                    //_lstResults.Add(new ObjResults(_SID, "BIL", _tmpSplit[1].Trim()));

                                    string _value = _tmpSplit[1].Trim();
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "BIL", _value, DateTime.Now));
                                }
                            }

                            else if (line.Contains("GLU"))
                            {
                                string[] _tmpSplit = Regex.Split(line, "GLU");
                                if (_tmpSplit.Length > 1)
                                {
                                    result += "GLU: " + _tmpSplit[1].Trim() + "\n";
                                    //_lstResults.Add(new ObjResults(_SID, "GLU", _tmpSplit[1].Trim()));

                                    string _value = _tmpSplit[1].Trim();
                                    _lstResults.Add(new ObjResults(_Ngay, _SID, _May, "GLU", _value, DateTime.Now));
                                }
                            }
                        }
                        sr.Close();


                        return _lstResults;
                    }
                }
                catch { return null; }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(ex.Message);
            }

            return null;
        }
    }
}
